package com.zm.zmgz.controller;

import com.zm.zmgz.pojo.ProjectProfitBatchOptionBean;
import com.zm.zmgz.pojo.Response;
import com.zm.zmgz.service.ReportManagementService;
import com.zm.zmgz.utils.DateUtils;
import com.zm.zmgz.utils.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: cjw
 * @Date: 2020/06/14/11:31
 * @Description: 报表管理  reportManagement
 */
@RestController
@RequestMapping("/reportManagement")
public class ReportManagementController {


    @Autowired
    ReportManagementService reportManagementService; // 报表管理业务层

    /***
     * @Description: 上传文件, 返回url
     * @Param:
     * @return:
     * @Author: cjw
     * @Date: 2020-06-15 09:06:26
     */
    @PostMapping("/upfile")
    public Response upfile(@RequestParam MultipartFile file, @RequestParam String name, HttpServletRequest request) {
        if (Objects.isNull(file))
            return new Response(20000, "上传文件为空!", null);
        String url = FileUtils.upfile(file, name, false, request);
        if (Objects.isNull(url))
            return new Response(20000, "上传文件失败!", null);
        else
            return new Response(20000, "上传文件成功!", url);

    }

    /***
     * @Description: 添加一条报表记录
     * @Param: batch:"2020年6月15日"
     * @return:
     * @Author: cjw
     * @Date: 2020-06-15 09:06:26
     */
//    @ButtonAuthorityAnnotation("/salarySettlement/salarySettlementIndex/generateReports")
    @PostMapping("/add")
    public Response add(@RequestHeader("X-Token") String token,@RequestParam String url, @RequestParam String batch, HttpServletRequest request) {
        if (Objects.isNull(url))
            return new Response(20000, "url为空!", false);
        if (Objects.isNull(batch))
            return new Response(20000, "批次为空!", false);
        Integer integer = reportManagementService.add(url, batch, request);
        return Objects.isNull(integer) || integer == 0 ? new Response(20000, "报表记录生成失败!", false) : new Response(20000, "报表记录生成成功!", true);

    }

    /***
     * @Description: 提供报表url集合
     * @Param:
     * @return:
     * @Author: cjw
     * @Date: 2020-04-22 17:42:03
     */
    @GetMapping("/getProjectProfitByDate")
    public Response getProjectProfitByDate(String date) {
        System.err.println(date);
        Date date1 = DateUtils.getDateByString(date,"yyyy年MM月");
        List<Map<String,String>>  list = reportManagementService.getBatchByDate(date1);
        return !Objects.isNull(list) ? new Response(20000, "获取报表成功!", list) : new Response(20000, "获取报表失败!", null);
    }


    /***
     * @Description: 获取批次选择数据(年月)
     * @Param:
     * @return:
     * @Author: cjw
     * @Date: 2020-04-22 17:42:03
     */
    @GetMapping("/getBatchOptions")
    public Response getBatchOptions() {
        List<ProjectProfitBatchOptionBean> list = reportManagementService.getBatchOptions();
        return !Objects.isNull(list) && list.size() > 0 ? new Response(20000, "获取批次选择数据成功!", list) : new Response(20000, "获取批次选择数据失败!", null);
    }

}
